#include <statMcDemo51.h>


#define staMcMgr51TranPro(sname) staMcMgr51TranstDemo(staMcEnumGet(sname))
typedef enum
{
    staMcEnumGet(morning) = 0,
    staMcEnumGet(breakfast),
    staMcEnumGet(working),
    staMcEnumGet(mysleep),
    staMcDoId_Max,
} staMcDoId_Enum;

static DEV_C51_XDATA_FLAG staMcCtxtStc smMgrInstDemo;

// #####################task morning begin############################
static void staMcFuncUpGet(morning)()
{
    if(rand() % 2)// late
    {
        logdebugc("get up late, go work first");
        staMcMgr51TranPro(working);
    }
    else
    {
        logdebugc("get up early, go breakfast first");
        staMcMgr51TranPro(breakfast);
    }
}
// #####################task morning end############################

// #####################task breakfast begin############################
static void staMcFuncUpGet(breakfast)()
{
    static sysTmTp bfNeedTime = 0;
    switch (smMgrInstDemo.substat)
    {
    default:
    case -1:// first in
    {
        smMgrInstDemo.substat = 0;
        bfNeedTime = rand() % 2000;
        smMgrInstDemo.runtmr = sysTmNow();
        logdebugc("breakfast need time:%d", (int)bfNeedTime);
        break;
    }
    case 0:
    {
        if(sysTmRunMs(smMgrInstDemo.runtmr) >= bfNeedTime)
        {
            logdebugc("breakfast finish, go work");
            staMcMgr51TranPro(working);
        }
        break;
    }
    }
}
// #####################task breakfast end############################

// #####################task working begin############################
static void staMcFuncUpGet(working)()
{
    if(sysTmRunMs(smMgrInstDemo.runtmr) < 3000)
    {
        return;
    }
    logdebugc("work off, go to sleep");
    staMcMgr51TranPro(mysleep);
}
// #####################task working end############################

// #####################task mysleep begin############################
static void staMcFuncUpGet(mysleep)()
{
    if(sysTmRunMs(smMgrInstDemo.runtmr) < 2000)
    {
        return;
    }
    logdebugc("wake up, get up now");
    staMcMgr51TranPro(morning);
}
// #####################task mysleep end############################


void staMcMgr51InitDemo()
{
    srand(sysTmNow());
    smMgrInstDemo.runtmr = sysTmNow();
    smMgrInstDemo.state = staMcEnumGet(morning);
    smMgrInstDemo.substat = -1;
}
void staMcMgr51QuitDemo()
{
}
void staMcMgr51UpDemo()
{
#define SM51DO_UP_FUNC(sname)    \
    case staMcEnumGet(sname):    \
    {                            \
        staMcFuncUpGet(sname)(); \
        break;                   \
    }
    switch (smMgrInstDemo.state)
    {
        SM51DO_UP_FUNC(morning);
        SM51DO_UP_FUNC(breakfast);
        SM51DO_UP_FUNC(working);
        SM51DO_UP_FUNC(mysleep);
    default:
    {
        break;
    }
    }
}
int staMcMgr51TranstDemo(int nextStateId)
{
    smMgrInstDemo.state = nextStateId;
    smMgrInstDemo.runtmr = sysTmNow();
    smMgrInstDemo.substat = -1;
    return 0;
}